﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKExcelFormTool.Utilities.Generics;

namespace YJKExcelFormTool.Utilities
{
	public class Common
	{
		public static double MMToFeet(double val)
		{
			return val / 304.8;
		}

		public static double FeetToMM(double val)
		{
			return val * 304.8;
		}

		public static int Chr2AscII(string character)
		{
			if (character.Length == 1)
			{
				return (int)new ASCIIEncoding().GetBytes(character)[0];
			}
			throw new Exception("Character is not valid.");
		}

		public static string AscII2Chr(int asciiCode)
		{
			if (asciiCode >= 0 && asciiCode <= 255)
			{
				ASCIIEncoding asciiencoding = new ASCIIEncoding();
				byte[] bytes = new byte[]
				{
					(byte)asciiCode
				};
				return asciiencoding.GetString(bytes);
			}
			throw new Exception("ASCII Code is not valid.");
		}

		public static List<XYZ> UniqueAndSortIntersectPoints(Curve curve, List<XYZ> intersectPoints)
		{
			Arc arc = curve as Arc;
			if (null != arc)
			{
				XYZ hyendPoint = arc.GetEndPoint(0);
				if (arc.Normal.Z < 0.0)
				{
					hyendPoint = arc.GetEndPoint(1);
				}
                intersectPoints.Sort(new YJKExcelFormTool.Utilities.Generics.AngleComparer(hyendPoint, arc.Center));
			}
			Line line = curve as Line;
			if (null != line)
			{
				XYZ hyendPoint2 = line.GetEndPoint(0);
				intersectPoints.Sort(new DistanceComparer(hyendPoint2));
			}
			IEnumerable<XYZ> enumerable = intersectPoints.Distinct(new Comparint<XYZ>((XYZ x, XYZ y) => x != null && y != null && x.DistanceTo(y) < 0.001));
			List<XYZ> list = new List<XYZ>();
			foreach (XYZ item in enumerable)
			{
				list.Add(item);
			}
			return list;
		}

		public static bool CreateViewDrafting(Autodesk.Revit.DB.Document doc, int scale, out ViewDrafting view)
		{
			view = RevitViewExtension.CreateViewDrafting(doc);
			if (view == null)
			{
				return false;
			}
			view.ApplyViewTemplateParameters(doc.ActiveView);
			BoundingBoxXYZ boundingBoxXYZ = new BoundingBoxXYZ();
			boundingBoxXYZ.Max = new XYZ(10.0, 10.0, 5.0);
			boundingBoxXYZ.Min = new XYZ(-10.0, -10.0, -5.0);
			view.CropBox = boundingBoxXYZ;
			view.Scale = scale;
			return true;
		}
	}
}
